//
//  WYMMainViewController.swift
//  iOS11weibo
//
//  Created by 王应明 on 16/7/18.
//  Copyright © 2016年 DescendantsOftheSun. All rights reserved.
//

import UIKit

class WYMMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        let mainTabBar = WYMMainTabBar()
        
        mainTabBar.composeCallback = {() -> Void in
            
            print("点击了按钮")
        }
        
        self.setValue(mainTabBar, forKey: "tabBar")
        
        self.addChildViewControllers()

    }

    //创建设置tabbar
    func addChildViewControllers() -> Void {
        let homeVC = WYMHomeTableViewController()
        self.addChildViewControllerall(homeVC, title: "首页", imageName: "tabbar_home")
        
        let messageVC = WYMMessageTableViewController()
        self.addChildViewControllerall(messageVC, title: "消息", imageName: "tabbar_message_center")
        
        let discoveryVC = WYMDiscoveryTableViewController()
        self.addChildViewControllerall(discoveryVC, title: "发现", imageName: "tabbar_discover")
        
        let profileVC = WYMProfileTableViewController()
        self.addChildViewControllerall(profileVC, title: "我", imageName: "tabbar_profile")
        
    }
    
    func addChildViewControllerall(controller: UIViewController, title: String, imageName: String ) -> Void {
        
        //加载首页控制器
        controller.title = title
        controller.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orangeColor()], forState: UIControlState.Selected)
        controller.tabBarItem.image = UIImage(named: imageName)
        //拼接选中图片的名称
        let selectedImageName = imageName + "_selected"
        //设置选中图片
        controller.tabBarItem.selectedImage = UIImage(named: selectedImageName)?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)//不要让他渲染

        let nav = UINavigationController(rootViewController: controller)
        self.addChildViewController(nav)
    }
    
}
